package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.converter.ConversionUtils;
import hcn.base.MsgPushTask;
import hcn.util.CommonUtils;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import pcn.bean.consultation.MsgPushTaskListQo;

/**
 * @description:手工消息推送任务表Dao层
 * @author: zhengcb10708
 * @create: 2021-09-01 16:25
 **/
@SsdevDao(serviceId = "msgPushTaskService")
public abstract class MsgPushTaskDao  extends HibernateSupportDelegateDAO<MsgPushTask> {
    public MsgPushTaskDao() {
        this.setKeyField("msgPushTaskId");
    }

    @DAOMethod(sql = "from MsgPushTask where msgPushTaskId=:msgPushTaskId")
    public abstract MsgPushTask getMsgPushTaskByTaskId(@DAOParam("msgPushTaskId") Long msgPushTaskId);

    public List<MsgPushTask> getMsgPushTaskList() {
        HibernateStatelessResultAction<List<MsgPushTask>> action =
                new AbstractHibernateStatelessResultAction<List<MsgPushTask>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer stringBuffer = new StringBuffer();
                        stringBuffer.append(" SELECT a.msg_push_task_id msgPushTaskId,a.msg_title msgTitle,a.push_object pushObject,a.head_count headCount,a.success_head_count successHeadCount, ");
                        stringBuffer.append(" a.task_status taskStatus,a.create_time createTime,a.last_modify_time lastModifyTime, ");
                        stringBuffer.append(" a.push_time_type pushTimeType,a.time_of_timer timeOfTimer,a.start_push_time startPushTime, ");
                        stringBuffer.append(" a.end_push_time endPushTime,a.msg_type msgType,a.link_type linkType,a.link_target linkTarget, ");
                        stringBuffer.append(" a.content content,a.push_scope pushScope ");
                        stringBuffer.append(" from msg_push_task a where 1=1 and a.task_status=1 and time_of_timer <= NOW() ");

                        Query query = ss.createSQLQuery(stringBuffer.toString());
                        query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);

                        List<Map> list = query.list();
                        List<MsgPushTask> voList = new ArrayList<MsgPushTask>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, MsgPushTask.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<MsgPushTask> getMsgPushTaskList(final MsgPushTaskListQo msgPushTaskListQo) {
        HibernateStatelessResultAction<List<MsgPushTask>> action =
                new AbstractHibernateStatelessResultAction<List<MsgPushTask>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer stringBuffer = new StringBuffer();
                        stringBuffer.append(" SELECT a.msg_push_task_id msgPushTaskId,a.msg_title msgTitle,a.push_object pushObject,a.head_count headCount,a.success_head_count successHeadCount,  ");
                        stringBuffer.append(" a.task_status taskStatus,a.create_time createTime,a.last_modify_time lastModifyTime, ");
                        stringBuffer.append(" a.push_time_type pushTimeType,a.time_of_timer timeOfTimer,a.start_push_time startPushTime, ");
                        stringBuffer.append(" a.end_push_time endPushTime,a.msg_type msgType,a.link_type linkType,a.link_target linkTarget, ");
                        stringBuffer.append(" a.content content,a.push_scope pushScope ");
                        stringBuffer.append(" from msg_push_task a where 1=1");

                        if (CommonUtils.isNotEmpty(msgPushTaskListQo.getMsgTitle())) {
                            stringBuffer.append( " and a.msg_title like :msgTitle  "  );
                        }
                        if (CommonUtils.isNotEmpty(msgPushTaskListQo.getPushObject())&&msgPushTaskListQo.getPushObject()!=0) {
                            stringBuffer.append( " and a.push_object =:pushObject  "  );
                        }
                        //99代表查询全部
                        if (CommonUtils.isNotEmpty(msgPushTaskListQo.getTaskStatus())&&msgPushTaskListQo.getTaskStatus()!=99) {
                            stringBuffer.append( " and a.task_status =:taskStatus  "  );
                        }
                        stringBuffer.append("   order by case when a.start_push_time is null then  (case when a.time_of_timer is null then a.last_modify_time else a.time_of_timer end) else start_push_time end desc,a.create_time desc ");

                        Query query = ss.createSQLQuery(stringBuffer.toString());

                        if (CommonUtils.isNotEmpty(msgPushTaskListQo.getMsgTitle())) {
                            query.setParameter("msgTitle",  "%" + msgPushTaskListQo.getMsgTitle() + "%");
                        }
                        if (CommonUtils.isNotEmpty(msgPushTaskListQo.getPushObject())&&msgPushTaskListQo.getPushObject()!=0) {
                            query.setParameter("pushObject",  msgPushTaskListQo.getPushObject());
                        }
                        if (CommonUtils.isNotEmpty(msgPushTaskListQo.getTaskStatus())&&msgPushTaskListQo.getTaskStatus()!=99) {
                            query.setParameter("taskStatus",  msgPushTaskListQo.getTaskStatus());
                        }

                        query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                        //分页
                        int first = msgPushTaskListQo.getPageNo() > 0 ? (msgPushTaskListQo.getPageNo() - 1) * msgPushTaskListQo.getPageSize() : 0;
                        query.setFirstResult(first);
                        query.setMaxResults(msgPushTaskListQo.getPageSize());

                        List<Map> list = query.list();
                        List<MsgPushTask> voList = new ArrayList<MsgPushTask>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, MsgPushTask.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public BigInteger getMsgPushTaskListCount(final MsgPushTaskListQo msgPushTaskListQo) {
        HibernateStatelessResultAction<BigInteger> action = new AbstractHibernateStatelessResultAction<BigInteger>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {

                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(" SELECT count(1) from msg_push_task a where 1=1");

                if (CommonUtils.isNotEmpty(msgPushTaskListQo.getMsgTitle())) {
                    stringBuffer.append( " and a.msg_title like :msgTitle  "  );
                }
                if (CommonUtils.isNotEmpty(msgPushTaskListQo.getPushObject())&&msgPushTaskListQo.getPushObject()!=0) {
                    stringBuffer.append( " and a.push_object =:pushObject  "  );
                }
                //99代表查询全部
                if (CommonUtils.isNotEmpty(msgPushTaskListQo.getTaskStatus())&&msgPushTaskListQo.getTaskStatus()!=99) {
                    stringBuffer.append( " and a.task_status =:taskStatus  "  );
                }
                stringBuffer.append("  order by case when a.start_push_time is null then  (case when a.time_of_timer is null then a.last_modify_time else a.time_of_timer end) else start_push_time end desc,a.create_time desc  ");

                Query query = ss.createSQLQuery(stringBuffer.toString());

                if (CommonUtils.isNotEmpty(msgPushTaskListQo.getMsgTitle())) {
                    query.setParameter("msgTitle",  "%" + msgPushTaskListQo.getMsgTitle() + "%");
                }
                if (CommonUtils.isNotEmpty(msgPushTaskListQo.getPushObject())&&msgPushTaskListQo.getPushObject()!=0) {
                    query.setParameter("pushObject",  msgPushTaskListQo.getPushObject());
                }
                if (CommonUtils.isNotEmpty(msgPushTaskListQo.getTaskStatus())&&msgPushTaskListQo.getTaskStatus()!=99) {
                    query.setParameter("taskStatus",  msgPushTaskListQo.getTaskStatus());
                }
                List list = query.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    BigInteger count = (BigInteger) o;
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
